﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Ue.Core.DynamicReflection
{
    public abstract class DynamicReflectionCache<TKey, TValue> : IDynamicReflectionCache<TKey, TValue>
    {
        private IDictionary<TKey, TValue> m_cache = new ConcurrentDictionary<TKey, TValue>();

        public TValue Get(TKey key)
        {
            TValue value = default(TValue);
            if (this.m_cache.TryGetValue(key, out value))
            {
                return value;
            }

            if (!this.m_cache.TryGetValue(key, out value))
            {
                value = this.Create(key);
                this.m_cache[key] = value;
            }


            return value;
        }

        protected abstract TValue Create(TKey key);
    }
}
